package com.xydtech.ecard.imp.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.domain.entity.SysUser;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.*;
import com.xydtech.common.utils.bean.ExcelImpError;
import com.xydtech.common.utils.file.FileUploadUtils;
import com.xydtech.common.utils.file.MimeTypeUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.common.utils.string.StringUtil;
import com.xydtech.ecard.imp.domain.EcardMerchantImport;
import com.xydtech.ecard.imp.domain.EcardMerchantImportTask;
import com.xydtech.ecard.imp.service.IEcardMerchantImportService;
import com.xydtech.ecard.imp.service.IEcardMerchantImportTaskService;
import com.xydtech.ecard.user.domain.EcardApiEcardRegistInfo;
import com.xydtech.ecard.user.service.IEcardApiEcardRegistInfoService;
import com.xydtech.system.service.ISysConfigService;
import com.xydtech.system.service.ISysDeptService;
import com.xydtech.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 个体工商户批量导入Controller
 *
 * @author xydtech
 * @date 2024-11-22
 */
@Controller
@RequestMapping("/ecard/imp/merchantImport")
public class EcardMerchantImportController extends BaseController {
    private String prefix = "ecard/imp/merchantImport";

    /**
     * 导入条数限制
     */
    private static int MIN_IMP_COUNT = 1;
    private static int MAX_IMP_COUNT = 5000;

    @Autowired
    private IEcardMerchantImportService ecardMerchantImportService;
    @Autowired
    private IEcardMerchantImportTaskService ecardMerchantImportTaskService;
    @Autowired
    private IEcardApiEcardRegistInfoService registInfoService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysConfigService sysConfigService;

    @RequiresPermissions("imp:merchantImport:view")
    @GetMapping()
    public String merchantImport(ModelMap mmap) {
        EcardMerchantImport ecardMerchantImport = new EcardMerchantImport();
        ecardMerchantImport.setWhetherAllocation("0");
        mmap.put("ecardMerchantImport", ecardMerchantImport);
        return prefix + "/merchantImport";
    }

    /**
     * 查询个体工商户批量导入列表
     */
    @RequiresPermissions("imp:merchantImport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardMerchantImport ecardMerchantImport) {
        ecardMerchantImport.setSalt(getSalt());
        startPage();
        List<EcardMerchantImport> list = ecardMerchantImportService.selectEcardMerchantImportList(ecardMerchantImport);
        return getDataTable(list);
    }

    /**
     * 导出个体工商户批量导入列表
     */
    @RequiresPermissions("imp:merchantImport:export")
    @Log(title = "个体工商户批量导入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardMerchantImport ecardMerchantImport) {
        ecardMerchantImport.setSalt(getSalt());
        List<EcardMerchantImport> list = ecardMerchantImportService.selectEcardMerchantImportList(ecardMerchantImport);
        ExcelUtil<EcardMerchantImport> util = new ExcelUtil<EcardMerchantImport>(EcardMerchantImport.class);
        return util.exportExcel(list, "个体工商户批量导入数据");
    }

    /**
     * 新增个体工商户批量导入
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存个体工商户批量导入
     */
    @RequiresPermissions("imp:merchantImport:add")
    @Log(title = "个体工商户批量导入", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardMerchantImport ecardMerchantImport) {
        //校验是否超级管理员
        SysDept dep = ShiroUtils.getSysUser().getDept();
        if (dep.getDeptId().intValue() == 100) {
            return AjaxResult.error("超级管理员不能新增数据，请切换法人行账号操作！");
        }
        //验证手机号是否正确
        boolean phone = PhoneUtils.isPhone(ecardMerchantImport.getPhoneNumber());
        if (!phone) {
            return AjaxResult.error("手机号不正确!");
        }

        //验证身份证号是否正确,没有身份证号时不验证
        boolean idCard = true;
        if (!StringUtil.isNullOrEmpty(ecardMerchantImport.getCardnum())) {
            idCard = IdcardUtils.validateCard(ecardMerchantImport.getCardnum());
        }
        if (!idCard) {
            return AjaxResult.error("身份证号不正确!");
        }

        try {
            String salt = getSalt();
            if (!StringUtil.isNullOrEmpty(ecardMerchantImport.getCardnum())) {
                ecardMerchantImport.setCardnum(AESEncryptionUtils.encrypt(ecardMerchantImport.getCardnum(), salt));
            }
            if (!StringUtil.isNullOrEmpty(ecardMerchantImport.getPhoneNumber())) {
                ecardMerchantImport.setPhoneNumber(AESEncryptionUtils.encrypt(ecardMerchantImport.getPhoneNumber(), salt));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        SysUser user = ShiroUtils.getSysUser();
        SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(user.getDeptId());
        //校验管户人行和归属支行
        if (StringUtil.isNullOrEmpty(ecardMerchantImport.getUserCode())) {
            if (StringUtil.isNullOrEmpty(ecardMerchantImport.getDeptName())) {
                return AjaxResult.error("管户人员和归属支行不能同时为空!");
            } else {
                //管户人员为空时校验归属支行是否存在
                List<SysDept> sysDeptList = sysDeptService.selectDeptByDeptName(ecardMerchantImport.getDeptName(), dept.getDeptId());
                if (sysDeptList.size() == 0) {
                    return AjaxResult.error("归属支行不存在!");
                } else {
                    ecardMerchantImport.setDeptId(sysDeptList.get(0).getDeptId());
                }
            }
        } else {
            //校验该手机号对应的商户是否建档
            EcardMerchantImport oldEcardMerchantImport = ecardMerchantImportService.selectEcardMerchantImportByPhoneNumber(ecardMerchantImport.getPhoneNumber());
            if (oldEcardMerchantImport != null) {
                return AjaxResult.error("该个体工商户已分配!");
            }

            //查询管户人所在机构
            SysUser sysUser = sysUserService.selectUserByUserCode(ecardMerchantImport.getUserCode());
            if (sysUser == null) {
                return AjaxResult.error("管户人工号不存在！");
            }
            SysDept sysDept = sysDeptService.selectDeptById(sysUser.getDeptId(), getSalt());
            ecardMerchantImport.setDeptId(sysDept.getDeptId());

        }
        //法人机构号
        ecardMerchantImport.setCorpId(dept.getDeptId());

        return toAjax(ecardMerchantImportService.insertEcardMerchantImport(ecardMerchantImport));
    }

    /**
     * 修改个体工商户批量导入
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        EcardMerchantImport ecardMerchantImport = ecardMerchantImportService.selectEcardMerchantImportById(id);
        String salt = getSalt();
        try {
            if (!StringUtil.isNullOrEmpty(ecardMerchantImport.getCardnum())) {
                ecardMerchantImport.setCardnum(AESEncryptionUtils.decrypt(ecardMerchantImport.getCardnum(), salt));
            }
            if (!StringUtil.isNullOrEmpty(ecardMerchantImport.getPhoneNumber())) {
                ecardMerchantImport.setPhoneNumber(AESEncryptionUtils.decrypt(ecardMerchantImport.getPhoneNumber(), salt));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mmap.put("ecardMerchantImport", ecardMerchantImport);
        return prefix + "/edit";
    }

    /**
     * 修改保存个体工商户批量导入
     */
    @RequiresPermissions("imp:merchantImport:edit")
    @Log(title = "个体工商户批量导入", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardMerchantImport ecardMerchantImport) {
        ecardMerchantImport.setSalt(getSalt());
        return toAjax(ecardMerchantImportService.updateEcardMerchantImport(ecardMerchantImport));
    }

    /**
     * 删除个体工商户批量导入数据
     */
    @RequiresPermissions("imp:merchantImport:remove")
    @Log(title = "删除个体工商户批量导入数据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ecardMerchantImportService.deleteEcardMerchantImportByIds(ids));
    }

    /**
     * 打开导入存量个体工商户信息页面
     *
     * @return
     */
    @RequiresPermissions("imp:merchantImport:edit")
    @Log(title = "打开导入存量个体工商户信息页面", businessType = BusinessType.UPDATE)
    @GetMapping("/openImportOldCustPage")
    public String openImportOldCustPage() {
        return prefix + "/import_old_cust";
    }

    /**
     * 打开导入新增个体工商户信息页面
     *
     * @return
     */
    @RequiresPermissions("imp:merchantImport:edit")
    @Log(title = "打开导入新增个体工商户信息页面", businessType = BusinessType.UPDATE)
    @GetMapping("/openImportNewCustPage")
    public String openImportNewCustPage() {
        return prefix + "/import_new_cust";
    }

    /**
     * 下载导入模板
     *
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequiresPermissions("imp:merchantImport:edit")
    @PostMapping(value = "/downloadImpOldTemplate")
    @Log(title = "下载导入模板", businessType = BusinessType.OTHER)
    @ResponseBody
    public AjaxResult downloadImpOldCustTemplate(HttpServletRequest request, HttpServletResponse response) {
        ExcelUtil<EcardMerchantImport> util = new ExcelUtil<>(EcardMerchantImport.class);
        return util.importTemplateExcel("个体工商户导入模板");
    }

    /**
     * 导入存量个体工商户信息
     * type 1:存量客户导入  2：新增客户导入
     * @param files
     * @return
     */
    @RequiresPermissions("imp:merchantImport:edit")
    @Log(title = "导入存量个体工商户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/importOldCust")
    @ResponseBody
    public AjaxResult importOldCust(@RequestParam("file") MultipartFile[] files,String type) {
        try {
            //校验是否超级管理员
            SysDept dep = ShiroUtils.getSysUser().getDept();
            if (dep.getDeptId().intValue() == 100) {
                return AjaxResult.error("超级管理员不能导入数据，请切换法人行账号导入数据！");
            }

            //校验是否上传了文件
            if (files == null || files.length < 1) {
                return AjaxResult.error("请选择文件进行上传！");
            }

            //校验文件的格式
            MultipartFile file = files[0];
            String fileType = FileUploadUtils.getExtension(file);
            if (!FileUploadUtils.isAllowedExtension(fileType, MimeTypeUtils.EXCEL_EXTENSION)) {
                return AjaxResult.error("请上传Excel格式的文件！");
            }

            ExcelUtil<EcardMerchantImport> util = new ExcelUtil<>(EcardMerchantImport.class);
            List<EcardMerchantImport> ecardMerchantImportList = util.importExcel(file.getInputStream());
            List<EcardMerchantImport> ecardMerchantImportData = new ArrayList<>();

            //校验导入文件的行数
            int rownums = ecardMerchantImportList.size();
            if (rownums < MIN_IMP_COUNT || rownums > MAX_IMP_COUNT) {
                return AjaxResult.error("上传的文件条数必须大于0条且不能超过5000条！");
            }

            //Excel不合规错误信息对象
            List<ExcelImpError> excelImpErrors = new ArrayList<>();
            SysUser user = ShiroUtils.getSysUser();
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(user.getDeptId());
            //查询法人机构下所有员工号
            //List<String> userCodeList = sysUserService.selectUserCodeList(dept.getDeptId());

            //查询所有已分配的商户手机号
            List<String> phoneNumberList = ecardMerchantImportService.selectPhoneNumberList();

                for (int i = 0; i < ecardMerchantImportList.size(); i++) {
                    //j用来标识行数，返回给页面时使用
                    int j = i + 1;
                    EcardMerchantImport ecardMerchantImport = ecardMerchantImportList.get(i);
                    ExcelImpError excelImpError = new ExcelImpError();
                    excelImpError.setName(ecardMerchantImport.getName());
                    excelImpError.setPhoneNumber(ecardMerchantImport.getPhoneNumber());
                    excelImpError.setUserCode(ecardMerchantImport.getUserCode());

                    //验证手机号是否正确
                    boolean phone = PhoneUtils.isPhone(ecardMerchantImport.getPhoneNumber());
                    //验证身份证号是否正确,没有身份证号时不验证
                    boolean idCard = true;
                    if (!StringUtil.isNullOrEmpty(ecardMerchantImport.getCardnum())) {
                        idCard = IdcardUtils.validateCard(ecardMerchantImport.getCardnum());
                    }
                    if (!phone) {
                        excelImpError.setRow(j);
                        excelImpError.setError(excelImpError.getError() + "手机号不正确!");
                        excelImpErrors.add(excelImpError);
                        continue;
                    }
                    if (!idCard) {
                        excelImpError.setRow(j);
                        excelImpError.setError(excelImpError.getError() + "身份证号不正确!");
                        excelImpErrors.add(excelImpError);
                        continue;
                    }
                    //手机号加密
                    String phoneNumber = AESEncryptionUtils.encrypt(ecardMerchantImport.getPhoneNumber(), getSalt());
                    ecardMerchantImport.setPhoneNumber(phoneNumber);
                    //身份证号加密
                    if (!StringUtil.isNullOrEmpty(ecardMerchantImport.getCardnum())) {
                        String cardnum = AESEncryptionUtils.encrypt(ecardMerchantImport.getCardnum(), getSalt());
                        ecardMerchantImport.setCardnum(cardnum);
                    }

                    //校验管户人行和归属支行
                    if (StringUtil.isNullOrEmpty(ecardMerchantImport.getUserCode())) {
                        if (StringUtil.isNullOrEmpty(ecardMerchantImport.getDeptName())) {
                            excelImpError.setRow(j);
                            excelImpError.setError(excelImpError.getError() + "管户人员和归属支行不能同时为空!");
                            excelImpErrors.add(excelImpError);
                            continue;
                        } else {
                            //管户人员为空时校验归属支行是否存在
                            List<SysDept> sysDeptList = sysDeptService.selectDeptByDeptName(ecardMerchantImport.getDeptName().trim(), dept.getDeptId());
                            if (sysDeptList.size() == 0) {
                                excelImpError.setRow(j);
                                excelImpError.setError(excelImpError.getError() + "归属支行不存在!");
                                excelImpErrors.add(excelImpError);
                                continue;
                            } else {
                                ecardMerchantImport.setDeptId(sysDeptList.get(0).getDeptId());
                            }
                        }
                    } else {
                        //校验该手机号对应的商户是否建档
                        //EcardMerchantImport oldEcardMerchantImport = ecardMerchantImportService.selectEcardMerchantImportByPhoneNumber(phoneNumber);
                        if (phoneNumberList.contains(phoneNumber)) {
                            excelImpError.setRow(j);
                            excelImpError.setError(excelImpError.getError() + "该个体工商户已分配!");
                            excelImpErrors.add(excelImpError);
                            continue;
                        }

                        //查询管户人所在机构
                        SysUser sysUser = sysUserService.selectUserByUserCode(ecardMerchantImport.getUserCode());
                        if (sysUser == null) {
                            excelImpError.setRow(j);
                            excelImpError.setError(excelImpError.getError() + "管户人工号不存在！");
                            excelImpErrors.add(excelImpError);
                            continue;
                        }
                        ecardMerchantImport.setDeptId(sysUser.getDeptId());

                    }

                    //法人机构号
                    ecardMerchantImport.setCorpId(dept.getDeptId());
                    ecardMerchantImportData.add(ecardMerchantImport);

                }

            if (ecardMerchantImportData.size() > 0) {
                ecardMerchantImportService.batchSaveOrUpdateMerchantImport(ecardMerchantImportData);
            }
            if (excelImpErrors.size() > 0) {
                return AjaxResult.error(excelImpErrors.size() + "条数据导入失败，详情见列表", excelImpErrors);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success("导入成功！");
    }


    /**
     * 选择一键分配的客户经理
     */
    @GetMapping("/allocationAll/")
    public String choiceUserByAllocation(ModelMap mmap) {
        SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        mmap.put("merchantType", "2");
        mmap.put("taskDays", sysConfigService.selectConfigByKey("sem.default.taskDays", dept.getDeptId()));
        return prefix + "/choiceUser";
    }

    /**
     * 选择自定义分配的客户经理
     */
    @GetMapping("/allocation/")
    public String choiceSingleUser(ModelMap mmap) {
        SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        mmap.put("merchantType", "2");
        mmap.put("taskDays", sysConfigService.selectConfigByKey("sem.default.taskDays", dept.getDeptId()));
        return prefix + "/choiceSingleUser";
    }


    /**
     * 个体工商户自定义分配,将选中的商户分配给选中的营销经理(只有一个)
     */
    @Log(title = "个体工商户自定义分配", businessType = BusinessType.UPDATE)
    @PostMapping("/defineTaskAllocation")
    @ResponseBody
    public AjaxResult defineTaskAllocation(Long[] ids, String merchantType, Long[] registIds, Long deptId, int taskDays) {

        List<EcardMerchantImportTask> list = new ArrayList<>();
        Date now = new Date();
        Date endDate = DateUtils.getNextDay(now, taskDays);
        Long userId = ShiroUtils.getUserId();
        for (int i = 0; i < ids.length; i++) {
            //校验执行状态，若该任务已完成，则不做自定义分配
            EcardMerchantImportTask oldImportTask = ecardMerchantImportTaskService.selectEcardMerchantImportTaskByImportId(ids[i]);
            if (oldImportTask != null) {
                continue;
            }
            EcardMerchantImportTask ecardMerchantImportTask = new EcardMerchantImportTask();
            ecardMerchantImportTask.setImportId(ids[i]);
            ecardMerchantImportTask.setMerchantType(merchantType);
            ecardMerchantImportTask.setDeptId(deptId);
            ecardMerchantImportTask.setCreateTime(now);
            ecardMerchantImportTask.setEndDate(endDate);
            //分配人userid
            ecardMerchantImportTask.setManagerId(userId);
            //微信端注册id
            ecardMerchantImportTask.setCmRegistId(registIds[0]);
            //任务状态  0待处理 1已完成 2超时完成
            ecardMerchantImportTask.setSts("0");
            list.add(ecardMerchantImportTask);
        }

        return toAjax(ecardMerchantImportTaskService.batchInsertImportTask(list));
    }

    /**
     * 个体工商户一键分配，按顺序将相应数量的商户分配给选中的营销经理
     */
    @Log(title = "个体工商户一键分配", businessType = BusinessType.UPDATE)
    @PostMapping("/allTaskAllocation")
    @ResponseBody
    public AjaxResult allTaskAllocation(String merchantType, String[] registIds, int taskDays, Integer taskNums) {
        //查询未分配的个体工商户数据（数据权限）
        EcardMerchantImport ecardMerchantImport = new EcardMerchantImport();
        ecardMerchantImport.setSalt(getSalt());
        ecardMerchantImport.setTaskNums(taskNums);
        //查询未分配的个体工商户
        List<EcardMerchantImport> list = ecardMerchantImportService.selectEcardMerchantImportNoAllocateList(ecardMerchantImport);
        if (list.size() == 0) {
            return AjaxResult.error("当前没有未分配的任务！");
        }

        // 任务总数
        int sum = list.size();
        // 待分配人员数
        int nums = registIds.length;

        //需要提取任务序数
        int index = 0;
        logger.info(sum + "条记录，分给" + nums + "人执行！");

        List<EcardMerchantImportTask> ecardMerchantImportTaskList = new ArrayList<>();
        Date now = new Date();
        Date endDate = DateUtils.getNextDay(now, taskDays);
        Long userId = ShiroUtils.getUserId();

        if (list != null && list.size() > 0) {
            // 循环分组
            for (int i = 0; i < sum; i++) {
                // 循环任务数据list， 每人分配一条任务就进行下一轮，直到任务分完为止
                for (int j = 0; j < nums; j++) {
                    index = i * nums + j;
                    // 超过总数，终止执行
                    if (index == sum) {
                        break;
                    }
                    EcardMerchantImportTask ecardMerchantImportTask = new EcardMerchantImportTask();
                    ecardMerchantImportTask.setImportId(list.get(index).getId());
                    ecardMerchantImportTask.setMerchantType(merchantType);
                    //ecardMerchantImportTask.setDeptId(deptId);
                    ecardMerchantImportTask.setCreateTime(now);
                    ecardMerchantImportTask.setEndDate(endDate);
                    //分配人userid
                    ecardMerchantImportTask.setManagerId(userId);
                    //微信端注册id
                    //ecardMerchantImportTask.setCmRegistId(registIds[0]);
                    //任务状态  0待处理 1已完成 2超时完成
                    ecardMerchantImportTask.setSts("0");

                    //选的客户经理和原客户经理重复则保持原揽存关系

                    if (list.get(index).getRegistId() != null && Arrays.asList(registIds).contains(list.get(index).getRegistId().toString())) {
                        ecardMerchantImportTask.setCmRegistId(list.get(index).getRegistId());
                    } else {
                        ecardMerchantImportTask.setCmRegistId(Long.valueOf(registIds[j]));
                    }
                    //查询部门编号
                    EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(Long.valueOf(registIds[j]), getSalt());
                    if (ecardApiEcardRegistInfo != null) {
                        SysUser sysUser = sysUserService.selectUserByUserCode(ecardApiEcardRegistInfo.getUserCode());
                        if (sysUser != null) {
                            ecardMerchantImportTask.setDeptId(sysUser.getDeptId());
                        }
                    }

                    ecardMerchantImportTaskList.add(ecardMerchantImportTask);
                }
                if (index == sum) {
                    break;
                }
            }
            if (ecardMerchantImportTaskList.size() > 0) {
                ecardMerchantImportTaskService.batchInsertImportTask(ecardMerchantImportTaskList);
            }
        }

        return AjaxResult.success();
    }

    /**
     * 转交
     *
     * @param mmap
     * @return
     */
    @GetMapping("/transfer/")
    public String transfer(ModelMap mmap) {
        mmap.put("merchantType", "2");
        return prefix + "/choiceSingleUser";
    }

    @Log(title = "转交", businessType = BusinessType.UPDATE)
    @PostMapping("/doTransfer")
    @ResponseBody
    public AjaxResult defineTaskAllocation(Long[] ids, String merchantType, Long[] registIds, Long deptId) {

        if (registIds[0] == null) {
            return AjaxResult.error("请选择移交人员！");
        }
        if (deptId == null) {
            return AjaxResult.error("请选择移交的机构！");
        }

        //查询移交对象
        EcardApiEcardRegistInfo ecardApiEcardRegistInfo = registInfoService.selectEcardApiEcardRegistInfoByRegistId(registIds[0], getSalt());
        if (ecardApiEcardRegistInfo == null) {
            return AjaxResult.error("该营销经理未在微信端注册！");
        }
        //移交列表
        List<EcardMerchantImport> ecardMerchantImportList = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            EcardMerchantImport ecardMerchantImport = new EcardMerchantImport();
            ecardMerchantImport.setId(ids[i]);
            ecardMerchantImport.setUserCode(ecardApiEcardRegistInfo.getUserCode());
            ecardMerchantImport.setDeptId(deptId);
            ecardMerchantImport.setRegistId(registIds[0]);
            ecardMerchantImportList.add(ecardMerchantImport);
        }
        return toAjax(ecardMerchantImportService.transfer(ecardMerchantImportList));

    }

}
